package com.moss.web;

import com.moss.common.core.BaseController;
import com.moss.common.domain.AjaxResult;
import com.moss.common.entity.SysRoleEntity;
import com.moss.system.service.RoleMenuService;
import com.moss.system.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gxcc3
 * @date: 2024/8/12 22:24
 */
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {

    @Resource
    RoleService roleService;

    @Resource
    RoleMenuService roleMenuService;


    /**
     * 获取角色列表
     * @param role 请求参数
     * @return AjaxResult<T>
     */
    @GetMapping("/list")
    public AjaxResult<List<SysRoleEntity>> getRoleList(SysRoleEntity role) {
        startPage();
        List<SysRoleEntity> list = roleService.getRoleList(role);
        return AjaxResult.success(list, new PageInfo<>(list).getTotal());
    }


    /**
     * 保存角色
     * @param role 保存数据
     * @return AjaxResult<T>
     */
    @PostMapping("/saveRole")
    public AjaxResult<Void> saveRole(@RequestBody SysRoleEntity role){
        role.setCreateBy(getUserId());
        roleService.saveRole(role);
        return AjaxResult.success();
    }

    /**
     * 获取最新排序号
     * @return AjaxResult<T>
     */
    @GetMapping("/findMaxSort")
    public AjaxResult<Integer> findMaxSort(){
        return AjaxResult.success(roleService.findMaxSort());
    }


    @GetMapping("/{roleId}")
    public AjaxResult<SysRoleEntity> getById(@PathVariable("roleId") Long roleId){
        return AjaxResult.success(roleService.getById(roleId));
    }

    @PostMapping("/updateRole")
    public AjaxResult<Void> updateRole(@RequestBody SysRoleEntity role){
        role.setUpdateBy(getUserId());
        roleService.updateRole(role);
        return AjaxResult.success();
    }

    @PostMapping("/updateRoleStatus")
    public AjaxResult<Void> updateRoleStatus(@RequestBody SysRoleEntity role){
        role.setUpdateBy(getUserId());
        roleService.updateRoleStatus(role);
        return AjaxResult.success();
    }

    @DeleteMapping("/{roleIds}")
    public AjaxResult<Void> deleteRole(@PathVariable Long[] roleIds){
        roleService.deleteRole(roleIds);
        return AjaxResult.success();
    }




}
